//
//  TranducerXunJianViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-12-10.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "TranducerXunJianViewController.h"

@interface TranducerXunJianViewController ()
{
    NSMutableArray *checkNameLBBS;
    NSMutableArray *types;
    NSMutableArray *checkIds;
    NSMutableArray *isNormals;
    NSMutableArray *remarkTFs;
    NSMutableArray *remarks;
}
@end

@implementation TranducerXunJianViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"变频器月度巡检表";
    [super viewDidLoad];
    types = [NSMutableArray array];
    self.sendUrl=[NSString stringWithFormat:addCheckECResult];
    
    NSArray *queryDataArr = @[
  @{@"id": @"1",@"floorname":@"B3F冷冻机房(右1)",@"type":@"CIMR-F7B4055-55K",@"rfid":@"<#string#>"},
  @{@"id": @"2",@"floorname":@"B3F冷冻机房(右2)",@"type":@"CIMR-F7B4055-55K",@"rfid":@"<#string#>"},
  @{@"id": @"3",@"floorname":@"B3F冷冻机房(右3)",@"type":@"CIMR-F7B4055-55K",@"rfid":@"<#string#>"},
  @{@"id": @"4",@"floorname":@"B3F冷冻机房(右4)",@"type":@"CIMR-F7B4055-55K",@"rfid":@"<#string#>"},
  @{@"id": @"5",@"floorname":@"7FA104机房",@"type":@"FR-F740-37K",@"rfid":@"<#string#>"},
  @{@"id": @"6",@"floorname":@"7FA103机房",@"type":@"FR-F740-40K",@"rfid":@"<#string#>"},
  @{@"id": @"7",@"floorname":@"1FAHU机房",@"type":@"FR-F740-18.5K",@"rfid":@"<#string#>"},
  @{@"id": @"8",@"floorname":@"1F银行区",@"type":@"FR-F740-30-CH",@"rfid":@"<#string#>"},
  @{@"id": @"9",@"floorname":@"2F国际会议厅",@"type":@"FR-A140E-30K",@"rfid":@"<#string#>"},
  @{@"id": @"10",@"floorname":@"2F多功能厅",@"type":@"FR-A140F-30K",@"rfid":@"<#string#>"},
  @{@"id": @"11",@"floorname":@"2FAHU机房",@"type":@"FR-F740E-22K",@"rfid":@"<#string#>"},
  @{@"id": @"12",@"floorname":@"3F AHU机房",@"type":@"FR-A740E-18.5K",@"rfid":@"<#string#>"},
  @{@"id": @"13",@"floorname":@"4F AHU机房",@"type":@"FR-F740E-11.5K",@"rfid":@"<#string#>"},
  @{@"id": @"14",@"floorname":@"5F AHU机房",@"type":@"FR-F740E-18.5K",@"rfid":@"<#string#>"},
  @{@"id": @"15",@"floorname":@"6F AHU机房",@"type":@"FR-F740E-22K",@"rfid":@"<#string#>"},
  @{@"id": @"16",@"floorname":@"7F AHU机房",@"type":@"FR-F740E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"17",@"floorname":@"7F排风机",@"type":@"CIMR-G5A4015",@"rfid":@"<#string#>"},
  @{@"id": @"18",@"floorname":@"7F排风机",@"type":@"FR-A140E-37K",@"rfid":@"<#string#>"},
  @{@"id": @"19",@"floorname":@"8F AHU机房",@"type":@"FR-F740E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"20",@"floorname":@"9F AHU机房",@"type":@"FR-F740E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"21",@"floorname":@"10F AHU机房",@"type":@"FR-F740E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"22",@"floorname":@"11F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"23",@"floorname":@"12F AHU机房",@"type":@"FR-F740-22K",@"rfid":@"<#string#>"},
  @{@"id": @"24",@"floorname":@"13F AHU机房",@"type":@"FR-FR540-15K",@"rfid":@"<#string#>"},
  @{@"id": @"25",@"floorname":@"14F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"26",@"floorname":@"15F AHU机房",@"type":@"FR-F740-18.5K",@"rfid":@"<#string#>"},
  @{@"id": @"27",@"floorname":@"16F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"28",@"floorname":@"17F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"29",@"floorname":@"18F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"30",@"floorname":@"19F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"31",@"floorname":@"20F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"32",@"floorname":@"21F AHU机房",@"type":@"FR-F540-15K",@"rfid":@"<#string#>"},
  @{@"id": @"33",@"floorname":@"22F AHU机房",@"type":@"FR-F740E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"34",@"floorname":@"23F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"35",@"floorname":@"24F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"36",@"floorname":@"25F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"37",@"floorname":@"26F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"38",@"floorname":@"27F AHU机房",@"type":@"FR-A140E-15K",@"rfid":@"<#string#>"},
  @{@"id": @"39",@"floorname":@"38F新风机",@"type":@"FR-F540-37K",@"rfid":@"<#string#>"},
  @{@"id": @"40",@"floorname":@"38F排风机",@"type":@"VLT-3000",@"rfid":@"<#string#>"},
  @{@"id": @"41",@"floorname":@"38F1#冷却塔（左)",@"type":@"VLT-3000",@"rfid":@"<#string#>"},
  @{@"id": @"42",@"floorname":@"38F1#冷却塔（中)",@"type":@"VLT-3000",@"rfid":@"<#string#>"},
  @{@"id": @"43",@"floorname":@"38F1#冷却塔（右)",@"type":@"VLT-3000",@"rfid":@"<#string#>"}];
    NSLog(@"查询%@",queryDataArr);
    self.floorNames = [NSMutableArray array];
    self.floorIds = [NSMutableArray array];
    for (NSDictionary *dic in queryDataArr)
    {
        [self.floorNames addObject:[dic valueForKey:@"floorname"]];
        [self.floorIds addObject:[dic valueForKey:@"id"]];
        [self.sendFloorDatas addObject:@""];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[dic valueForKey:@"id"] }];
        [types addObject:[dic objectForKey:@"type"]];
    }
    [self initData];
    [self showUIWithArray:self.allShowDataArr andFloorId:@"1"];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
- (void)initData
{
    self.allShowDataArr = @[@"开关温度",@"噪音",@"冷却风扇",@"LED显示屏",@"清洁",@"A相电流",@"B相电流",@"C相电流"];
    checkNameLBBS = [NSMutableArray array];
    checkIds = [NSMutableArray array];
}
- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    [self clearAll];
    self.floorIdQuanju = floorId;
    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObjectss:self.floorIdQuanju]];
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        self.sendCheckDatas = [receiveFloorObj objectForKey:@"checkData"];
    }
    else
    {
        self.sendCheckDatas = [NSMutableArray array];
        NSDictionary *sendDic = [NSDictionary dictionary];
        for (int i = 0; i < array.count; i ++)
        {
            if (i == 0 || i == 5 || i == 6 || i == 7)
            {
                sendDic = @{@"checkId": [NSString stringWithFormat:@"%d",i+1],@"result":@""};

            }
            else
            {
                sendDic = @{@"checkId": [NSString stringWithFormat:@"%d",i+1],@"result":@"3"};
            }
            [self.sendCheckDatas addObject:sendDic];
            [checkIds addObject:[NSString stringWithFormat:@"%d",i+1]];
        }
    }
    checkNameLBBS = [NSMutableArray array];
    isNormals = [NSMutableArray array];
    remarkTFs = [NSMutableArray array];
    remarks = [NSMutableArray array];
    UIImage *textImage = [UIImage imageNamed:@"输入框底"];
    UILabel *typeLable = [UILabel new];
       typeLable.text = [NSString stringWithFormat:@"型号：%@",[types objectAtIndex:[self.floorIds indexOfObjectss:floorId]]];
    typeLable.font = [UIFont systemFontOfSize:25];
    CGSize chSize = [typeLable.text sizeWithFont:typeLable.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    typeLable.frame = CGRectMake(100, 10, chSize.width, chSize.height);
    [self.backScrollV addSubview:typeLable];
    [checkNameLBBS addObject:typeLable];
    [self.controlWillClears addObject:typeLable];
    for (int i = 0; i <array.count; i ++)
    {
        [remarks addObject:@""];
        NSString *checkName = [array objectAtIndex:i];
        UILabel *checkNameLb = [UILabel new];
        checkNameLb.text = [NSString stringWithFormat:@"%@：",checkName];
        checkNameLb.font = [UIFont systemFontOfSize:25];
        chSize = [checkNameLb.text sizeWithFont:checkNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        checkNameLb.frame = CGRectMake(10, [[self.controlWillClears lastObject]frame].origin.y + [[self.controlWillClears lastObject]frame].size.height + interval111, chSize.width, chSize.height);
        [self.backScrollV addSubview:checkNameLb];
        [checkNameLBBS addObject:checkNameLb];
        [self.controlWillClears addObject:checkNameLb];
        if (i == 0 || i == 5 || i == 6 || i == 7)
        {
            UITextField *checkTf = [[UITextField alloc]initWithFrame:CGRectMake(150, checkNameLb.frame.origin.y, 380, chSize.height)];
            checkTf.keyboardType = UIKeyboardTypeNumberPad;
            checkTf.background = textImage;
            checkTf.delegate = self;
            checkTf.text = [[self.sendCheckDatas objectAtIndex:i]valueForKey:@"result"];
            [self.backScrollV addSubview:checkTf];
            [self.controlWillClears addObject:checkTf];
            [remarkTFs addObject:checkTf];
            [isNormals addObject:@"1"];
        }
        else
        {
            [self addSingleSelect:@[@"正常",@"异常"]andResult:[[self.sendCheckDatas objectAtIndex:i]valueForKey:@"result"]];
            if ([[[self.sendCheckDatas objectAtIndex:i]valueForKey:@"result"]isEqualToString:@"0"] || [[[self.sendCheckDatas objectAtIndex:i]valueForKey:@"result"]isEqualToString:@"1"] ||[[[self.sendCheckDatas objectAtIndex:i]valueForKey:@"result"]isEqualToString:@"3"])
            {
                [self addRemarkKuang:@""];
            }
            else
            {
                [self addRemarkKuang:[[self.sendCheckDatas objectAtIndex:i]valueForKey:@"result"]];
            }
            [isNormals addObject:@"0"];
        }
    }
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        [self addCommentKuang:[receiveFloorObj objectForKey:@"floorRemark"]];
    }
    else
    {
        [self addCommentKuang:@""];
    }
}
- (void)selectOptions:(id)sender//工程部
{
    UIButton *btn = (UIButton*)sender;
    for (int i = 0;i<self.selectBtnArr.count;i++)
    {
        NSArray *btnArr = [self.selectBtnArr objectAtIndex:i];
        if ([btnArr containsObject:btn])
        {
            for (int j = 0;j<btnArr.count;j++)
            {
                UIButton *seleBtn = [btnArr objectAtIndex:j];
                [seleBtn setImage:self.roundUnselect forState:UIControlStateNormal];
                if (btn == [btnArr objectAtIndex:j])
                {
                    [seleBtn setImage:self.roundSelect forState:UIControlStateNormal];
                    [self.seleBtnData replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%d",j]];
                    [isNormals replaceObjectAtIndex:i+1 withObject:[NSString stringWithFormat:@"%d",j]];
                    UITextField *tf = [remarkTFs objectAtIndex:i+1];
                    if (j == 0)
                    {
                        [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
                        tf.text = @"";
                    }
                    else if(j == 1)
                    {
                        [tf becomeFirstResponder];
                    }
                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[checkIds objectAtIndex:i+1],@"checkId",[self.seleBtnData objectAtIndex:i],@"result",nil];
                    [self.sendCheckDatas replaceObjectAtIndex:i+1 withObject:dic];
                }
            }
        }
    }
    NSLog( @"%@",self.sendCheckDatas);
}
- (void)addRemarkKuang:(NSString *)str
{
    UILabel *remarkLb = [UILabel new];
    remarkLb.text = @"备注：";
    remarkLb.font = [UIFont systemFontOfSize:25];
    CGSize mySz = [remarkLb.text sizeWithFont:remarkLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    remarkLb.frame = CGRectMake(100, [[self.controlWillClears lastObject]frame].origin.y + [[self.controlWillClears lastObject]frame].size.height + interval111, mySz.width, mySz.height);
    [self.backScrollV addSubview:remarkLb];
    [self.controlWillClears addObject:remarkLb];
    
    UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(190, remarkLb.frame.origin.y, 340, mySz.height)];
    tf.delegate = self;
    if ([str isEqualToString:@"0"]||[str isEqualToString:@"1"]||[str isEqualToString:@"3"])
    {
        tf.text = @"";
    }
    else
    {
        tf.text = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    tf.background = [UIImage imageNamed:@"输入框底"];
    [self.backScrollV addSubview:tf];
    [self.controlWillClears addObject:tf];
    [remarkTFs addObject:tf];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return self.floorNames.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (tableView == self.myTableView) {
        self.mycell.textLabel.text = [self.floorNames objectAtIndex:indexPath.row];
        return self.mycell;
    }
    return nil;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self hiddenAll];
    if (tableView == self.myTableView)
    {
        [self showUIWithArray:self.allShowDataArr andFloorId:[self.floorIds objectAtIndex:indexPath.row]];
    }
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//    if (textField.tag == 250)
//    {
//        return YES;
//    }
//    else
//    {
        for (int k = 0; k < remarkTFs.count; k ++)
        {
            id tf = [remarkTFs objectAtIndex:k];
            BOOL isNormal = [[isNormals objectAtIndex:k]integerValue];
            if (isNormal == YES && tf == textField)
            {
                return YES;
            }
//        }
    }    return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    for (int k = 0; k < remarkTFs.count; k ++)
    {
        UITextField *tf = [remarkTFs objectAtIndex:[checkIds indexOfObject:[NSString stringWithFormat:@"%d",k+1]]];
        if (tf == textField) {
            [remarks replaceObjectAtIndex:k withObject:[textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[checkIds objectAtIndex:k],@"checkId",[remarks objectAtIndex:k],@"result",nil];
            [self.sendCheckDatas replaceObjectAtIndex:k withObject:dic];
            NSLog(@"%@",self.sendCheckDatas);
        }
    }
}
#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
